﻿using Unity.Common;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CustomControls.Controls
{
    /// <summary>
    /// SwitchLight.xaml 的交互逻辑
    /// </summary>
    public partial class SwitchLight : UserControl
    {
        private bool CurrentState;

        static SwitchLight()
        {
            ElementBase.DefaultStyle<SwitchLight>(DefaultStyleKeyProperty);
        }

        #region 属性
        public Color LightColor
        {
            get { return (Color)GetValue(LightColorProperty); }
            set { SetValue(LightColorProperty, value); }
        }

        public static readonly DependencyProperty LightColorProperty =
            DependencyProperty.Register("LightColor", typeof(Color), typeof(SwitchLight), new PropertyMetadata(Brushes.Gray.Color));

        public bool State
        {
            get { return (bool)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }

        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register("State", typeof(bool), typeof(SwitchLight),
                new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(SwitchLight.StateChanged)));

        private static void StateChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            SwitchLight ObjectControl = (SwitchLight)dependencyObject;
            ObjectControl.SetLight(bool.Parse(e.NewValue.ToString()));
        }

        public string Info
        {
            get { return (string)GetValue(InfoProperty); }
            set { SetValue(InfoProperty, value); }
        }

        public static readonly DependencyProperty InfoProperty =
            DependencyProperty.Register("Info", typeof(string), typeof(SwitchLight), new PropertyMetadata(string.Empty));
        #endregion

        private void SetLight(bool flag)
        {
            if (CurrentState == flag)
            {
                return;
            }
            if (flag)
            {
                LightColor = Brushes.Green.Color;
            }
            else
            {
                LightColor = Brushes.Red.Color;
            }
            CurrentState = flag;
        }
    }
}
